import { FormBuilder, Validators, FormGroup } from '@angular/forms'
import { ApiService } from '../../services/api.service'
import { QlMessageService } from 'qloud-angular'
import { Component, OnInit, Output, EventEmitter, ViewChild, ViewContainerRef } from '@angular/core'
import { SearchComponent } from '../../components/search/search.component'
import { ModalService } from '@app/shared/utils'
import { ModalConfirmComponent } from '@app/shared/components/modal-confirm/modal-confirm.component'
import { NetService, PageService } from '@app/core/http'
import { Router } from '@angular/router'
import { CursorService } from '@app/core/http/cursor.service'
import { Store } from '@ngxs/store'
import { DictService } from '@app/shared/utils/dict.service'

@Component({
  selector: 'app-my-assign',
  templateUrl: './my-assign.component.html',
  styleUrls: ['./my-assign.component.css'],
  providers: [ApiService]
})
export class MyAssignComponent implements OnInit {
  public orgNameParam:any
  public managerNameParam:any
  public managerRolenoParam:any

  public addDistribution:any = ''
  public yghrData = [{ value: '李娜', label: '李娜' }, { value: '张建国', label: '张建国' }]
  public yghjsData = [{ value: '主管户', label: '主管户' }]
  public sffpghrData = [{ value: '是', label: '是' }, { value: '否', label: '否' }]
  public sfdtData = [{ value: '是', label: '是' }, { value: '否', label: '否' }]
  public aumData = [{ value: '大于等于', label: '大于等于' }, { value: '小于等于', label: '小于等于' }]
  public aumData2 = [{ value: '大于等于', label: '大于等于' }, { value: '小于等于', label: '小于等于' }]
  public khghData = [{ value: '辅管客户关系', label: '辅管客户关系' }]

  public myassignList = []

  public formGroup: FormGroup

  constructor(
    private message: QlMessageService,
    private apiService: ApiService,
    private fb: FormBuilder,
    public modal: ModalService,
    public pageService: PageService,
    public dictService: DictService,
    public cursorService: CursorService,
    private router: Router,
    private store: Store
    ) {
      this.formGroup = fb.group({
        customerName:[null],
        certType: [null],
        certNo:[null],
        customerLevel:[null],
        birthday:[null],
        aum:[null],
        assetBal:[null],
      })
    }

  ngOnInit() {
    this.onRefresh()
  }


  public transform(key: string, value: string, sql: string) {
    if (sql != 'select *') {
      sql += ' and ' + key + ' = ' + "'*" + value + "*'"
    } else {
      sql += ' where ' + key + ' = ' + "'*" + value + "*'"
    }
    return sql
  }

  public chooseCustomer(){
    this.onRefresh()
  }

  public onRefresh(){
    if (this.formGroup.value.customerName !='' && this.formGroup.value.customerName != undefined) {
      this.cursorService.query().eq('customername',this.formGroup.value.customerName)
    }
    if (this.formGroup.value.certType !='' && this.formGroup.value.certType != undefined) {
      // sql = this.transform('certType', this.formGroup.value.certType, sql)
      this.cursorService.query().eq('certtype',this.formGroup.value.certType)
    }
    if (this.formGroup.value.certNo !='' && this.formGroup.value.certNo != undefined) {
      this.cursorService.query().eq('certno',this.formGroup.value.certNo)
    }
    if (this.formGroup.value.customerLevel !='' && this.formGroup.value.customerLevel != undefined) {
      this.cursorService.query().eq('customerlevel',this.formGroup.value.customerLevel)
    }
    if (this.formGroup.value.birthday !='' && this.formGroup.value.birthday != undefined) {
      this.cursorService.query().eq('birthday',this.formGroup.value.birthday)
    }
    if (this.formGroup.value.aum !='' && this.formGroup.value.aum != undefined) {
      this.cursorService.query().eq('aum',this.formGroup.value.aum)
    }
    if(this.formGroup.value.assetBal !='' && this.formGroup.value.assetBal != undefined){
      this.cursorService.query().eq('assetbal',this.formGroup.value.assetBal)
    }
    // this.cursorService.statement = sql
    this.apiService.getCustomerList(this.cursorService).subscribe(data => {
      this.myassignList = data
    })
  }
  public sendDistribution(){
    const param = {
      "orgName":this.orgNameParam,
      "managerName":this.managerNameParam,
	    "managerRoleNo":this.managerRolenoParam
    }
    for (var i=0;i<this.addDistribution;i++) {
      this.apiService.senddistribute(param).subscribe(data => {
        this.message.success("操作成功")
      })
    }
  }

  public onCheckChange(event){
    this.addDistribution = event.size
  }

  public resetFun(){
    this.orgNameParam = ''
    this.managerNameParam = ''
    this.managerRolenoParam = '0'
  }
}
